﻿using System.Collections.Generic;
using System.Linq;
using Domain.Models;
using UI.Views;

namespace UI.Presenters
{
    // MVVM
    public class TestPresenter
    {
        private ITestView view;
        private Dictionary<string, string> inputedAnswers;
        private VariantsWorker variantsWorker;
        private List<Variant> variants;
        private int currentVariant;
        private SubjectsHandler subjectsHandler;
        private readonly List<Subject> subjects;


        public TestPresenter(ITestView view)
        {
            this.view = view;

            subjectsHandler = new SubjectsHandler(new BinarySaver<Subject>());

            subjects = subjectsHandler.LoadAllSubjects();

            foreach (Subject subject in subjects)
            {
                view.AddSubjectToComboBox(subject.Name);
            }

            inputedAnswers = new Dictionary<string, string>();

            variantsWorker = new VariantsWorker(new BinarySaver<Variant>());
        }

        public void LoadVariant()
        {
            view.ClearVariantCombobox();
            variants = variantsWorker.LoadAllVariants();
            variants.Sort((x, y) => x.Number < y.Number ? -1 : 1);
            foreach (var variant in variants)
                view.AddVariantToCombobox(variant.Number);
        }

        public void StartTest(int variantNumber)
        {
            view.Clear();
            currentVariant = variantNumber;
            foreach (var question in GetCurrentSubject().GetVariants()[currentVariant].Questions)
                view.AddQuestion(question.Text, question.Answers.Select(inputedAnswer => inputedAnswer.Text).ToList());
        }

        private Subject GetCurrentSubject()
        {
            return subjectsHandler.GetSubjectByName(view.GetCurrentSubjectName(),subjects);
        }

        public void EndTest()
        {
            if (GetCurrentSubject().GetVariants()[currentVariant].Questions.Count != inputedAnswers.Count)
                if (view.AskToEndTest() == false)
                    return;

            foreach (var question in GetCurrentSubject().GetVariants()[currentVariant].Questions)
                if (inputedAnswers.ContainsKey(question.Text))
                    GetCurrentSubject().GetVariants()[currentVariant].AnswerQuestion(question.Text, inputedAnswers[question.Text]);


            view.HideEndTestButton();
            view.ShowResult(GetCurrentSubject().GetVariants()[currentVariant].GetNumberOfTrueAnwers().ToString());
        }

        public void ChangeAnswer(string question, string answer)
        {
            inputedAnswers[question] = answer;
        }

        public void LoadVariantsOfSubject(string subjectName)
        {
            view.Clear();

            FillViewWithVariantsOfSubject(subjectsHandler.GetSubjectByName(subjectName, subjects));
        }

        private void FillViewWithVariantsOfSubject(Subject subject)
        {
            foreach (Variant variant in subject.GetVariants())
            {
                view.AddVariantToComboBox(variant.Number);
            }
        }
    }
}